"use strict";

/**
 * 通用库
 *
 */

/** 消息状态码 */
const web_status = {
    isSuccess: function (status) {
        return status >= 200 && status < 300;
    }
};

/** 弹窗状态码 */
const modal_status = {
    SUCCESS:  "success",
    FAIL:     "error",
    WARNING: "warning"
};

!function ($) {
    //辅助功能扩展

    // 显示图标
    function icon(type) {
        var icon = "";
        if (type == modal_status.WARNING) {
            icon = 0;
        } else if (type == modal_status.SUCCESS) {
            icon = 1;
        } else if (type == modal_status.FAIL) {
            icon = 2;
        } else {
            icon = 3;
        }
        return icon;
    }
    $.extend({
        // 弹出层封装处理
        modal: {
            // 消息提示
            msg: function(content, type) {
                if (type != undefined) {
                    layer.msg(content, { icon: icon(type), time: 1000, shift: 5 });
                } else {
                    layer.msg(content);
                }
            },
            // 错误消息
            msgError: function(content) {
                $.modal.msg(content, modal_status.FAIL);
            },
            // 成功消息
            msgSuccess: function(content) {
                $.modal.msg(content, modal_status.SUCCESS);
            },
            // 警告消息
            msgWarning: function(content) {
                $.modal.msg(content, modal_status.WARNING);
            },

            // 弹出提示
            alert: function(content, type) {
                layer.alert(content, {
                    icon: icon(type),
                    title: "系统提示",
                    btn: ['确认'],
                    btnclass: ['btn btn-primary'],
                });
            },
            // 错误提示
            alertError: function(content) {
                $.modal.alert(content, modal_status.FAIL);
            },
            // 成功提示
            alertSuccess: function(content) {
                $.modal.alert(content, modal_status.SUCCESS);
            },
            // 警告提示
            alertWarning: function(content) {
                $.modal.alert(content, modal_status.WARNING);
            },
            // 确认窗体
            confirm: function (content, callBack) {
                layer.confirm(content, {
                    icon: 3,
                    title: "系统提示",
                    btn: ['确认', '取消'],
                    btnclass: ['btn btn-primary', 'btn btn-danger'],
                }, function (index) {
                    layer.close(index);
                    callBack(true);
                });
            },

            //加载层
            loading: function (text) {
                return layer.load(0, {
                    shade: [0.5, 'gray'],
                    content: text || '加载中...',
                    success: function (layero) {
                        layero.find('.layui-layer-content').css({
                            width: 'auto',
                            height: '32px',
                            backgroundPosition: 'center',
                            paddingTop: '32px',
                            textAlign: 'center'
                        })
                    },
                    //time: 5000
                })
            },
            //关闭加载层
            closeLoading: function (index) {
                //layer.index: 最近的弹出层
                layer.close(index || layer.index);
            }
        }
    })

    /**
     * jQuery Ajax 全局设置
     */
    $.ajaxSetup({
        type: 'post',             //POST方法
        dataType: 'json',        //预期返回数据类型
        timeout: 5000,            //超时时间
        //网络或服务器异常时调用
        error: function(request, textStatus, errorThrown) {
            if (textStatus == "parsererror") {
                $.modal.alertError("JSON解析错误")
            } else {
                //responseJSON的格式取决于服务器端BasicErrorController
                if (request.responseJSON && request.responseJSON.message) {
                    $.modal.alertError(request.responseJSON.message);
                }
            }
        }
    });

    /**
     * 登陆注册密码显示隐藏代码
     */
    $(":password").each(function () {
        let $password = $(this);
        let $span = $password.next("span.glyphicon");
        $span.click(function () {
            if ($span.hasClass("glyphicon-eye-close")){
                $span.removeClass("glyphicon-eye-close");
                $span.addClass("glyphicon-eye-open");

                $password.attr("type","text")
            } else {
                $span.removeClass("glyphicon-eye-open");
                $span.addClass("glyphicon-eye-close");

                $password.attr("type","password")
            }
        })
    })
}(jQuery);